/**
 * 初始化协议明细详情对话框
 */
var userDeptSelect = {
    instance: null,
    userDeptSelectData : {}
};

/**
 * 关闭此对话框
 */
userDeptSelect.close = function() {
    $("#userDeptMenuContent").css("display","none");
}

/**
 * 点击部门input框时
 *
 * @param e
 * @param treeId
 * @param treeNode
 * @returns
 */
userDeptSelect.onClickDept = function (e, treeId, treeNode) {
    $("#userName").attr("value", userDeptSelect.instance.getSelectedVal());
    $("#userId").attr("value", treeNode.id);
};

/**
 * 显示部门选择的树
 *
 * @returns
 */
userDeptSelect.showDeptSelectTree = function () {
    var cityObj = $("#userName");
    var cityOffset = $("#userName").offset();
    $("#userDeptMenuContent").css({
        left: cityOffset.left + "px",
        top: cityOffset.top + cityObj.outerHeight() + "px"
    }).slideDown("fast");

    $("body").bind("mousedown", userDeptSelect.onBodyDown);
};

/**
 * 显示用户详情部门选择的树
 *
 * @returns
 */
userDeptSelect.showInfoDeptSelectTree = function () {
    var cityObj = $("#userName");
    var cityPosition = $("#userName").position();
    $("#userDeptMenuContent").css({
        left: cityPosition.left + "px",
        top: cityPosition.top + cityObj.outerHeight() + "px"
    }).slideDown("fast");

    $("body").bind("mousedown", userDeptSelect.onBodyDown);
};

/**
 * 隐藏部门选择的树
 */
userDeptSelect.hideDeptSelectTree = function () {
    $("#userDeptMenuContent").fadeOut("fast");
    $("body").unbind("mousedown", userDeptSelect.onBodyDown);// mousedown当鼠标按下就可以触发，不用弹起
};

userDeptSelect.onBodyDown = function(event) {
    if (!(event.target.id == "menuBtn" || event.target.id == "menuContent" || $(
            event.target).parents("#userDeptMenuContent").length > 0)) {
        userDeptSelect.hideDeptSelectTree();
    }
}

$(function() {
    var menuHtml = '<div id=\"userDeptMenuContent\" class=\"menuContent\" style=\"display: none; position: absolute; z-index: 200;\"> <ul id=\"userDeptTreeDemo\" class=\"ztree tree-box\" style=\"width: 249px !important;\"></ul></div>';
    $("body").prepend(menuHtml);
    var userDeptZtree = new $ZTree("userDeptTreeDemo", "/mgr/userDeptZTreeList");
    userDeptZtree.bindOnClick(userDeptSelect.onClickDept);
    userDeptZtree.init();
    userDeptSelect.instance = userDeptZtree;
});
